
/*
 * WFSensor.h
 *
 *  Created on: Nov 5, 2021
 *  Author: ITry
 *  Version: 1.2
 */

#ifndef __WFSENSOR_H__
#define __WFSENSOR_H__

#include "public.h"
#include "SoftIIC.h"

/* 基本类型定义 */
typedef signed char         int8_t;
typedef unsigned char       uint8_t;
typedef signed short int    int16_t;
typedef unsigned short int  uint16_t;
//typedef signed int          int32_t;
//typedef unsigned int        uint32_t;
typedef signed long long    int64_t;
typedef unsigned long long  uint64_t;
typedef int                 bool;
#define true                1
#define false               0

/* 输出double精度，默认float */
//#define DOUBLE_PRECISION_FLOATING_POINT

#ifdef DOUBLE_PRECISION_FLOATING_POINT
typedef double wf_t;
#else
typedef float wf_t;
#endif

/* 传感器型号 */
typedef enum {
	WF5803F_1BAR,
	WF5803F_2BAR,
	WF5803F_7BAR,
	WF100D_5KPA,
	WF100D_10KPA,
	WF100D_40KPA,
	WF100D_100KPA,
	WF100D_200KPA,
	WF100D_300KPA,
	WF200D_5KPA
} sensor_model;

/* 传感器压力单位 */
typedef enum {
    bar =	0x00,
    kPa,		//千帕:				1bar=100
    hPa,		//百千帕:			1bar=1000
    mbar,		//毫巴:				1bar=1000
    Pa,			//帕斯卡:			1bar=100000
    PSI,		//磅力/英寸^2:		1bar=14.503774
    kgf_cm2,	//公斤力/厘米^2:	1bar=1.0197162
    Torr		//毫米汞柱(托):		1bar=750.06168
} press_measurement_unit;

/* 传感器通讯总线方式 */
typedef enum {
    IIC			=	0x01,
    SPI_3WIRE,
    SPI_4WIRE
} bus_mode;

/* 传感器通讯总线接口 */
typedef struct bus_handler {
    int (*read_byte) (void* bus_param, uint8_t dev_addr, uint8_t reg_addr, uint8_t *data);
    int (*read_bytes)(void* bus_param, uint8_t dev_addr, uint8_t reg_addr, uint8_t len, uint8_t *data);
    int (*write_byte)(void* bus_param, uint8_t dev_addr, uint8_t reg_addr, uint8_t data);
    void* bus_param;
    void (*wf_delayMs) (uint16_t t);//毫秒级别的延时函数
} bus_handler;

/* 采样数据 */
typedef struct sensor_data {
    wf_t pressure;			//压力数据
    wf_t temperature;		//温度数据
    int64_t timestamp;		//数据检索时的时间戳
    uint8_t status;			//转换状态
} sensor_data;

typedef struct i2c
{
    void (*sda_in)();
    void (*sda_out)();
    void (*set_sda)();
     void (*clr_sda)();
     void (*set_scl)();
     void (*clr_scl)();
     uint8_t (*sda_h)();
     uint8_t (*sda_l)();
     uint8_t (*scl_h)();	
} i2c;
/*=============================================================================
---函数：sensor_init(sensor_model model, press_measurement_unit unit, bus_mode mode, const bus_handler* handler)
---功能：传感器初始化配置
---输入：model		- 传感器型号
		 unit		- 压力单位
         mode		- 通讯方式
         handler	- 通讯总线接口
---输出：通讯总线的返回结果
-----------------------------------------------------------------------------*/
int sensor_init(sensor_model model, press_measurement_unit unit, bus_mode mode, const bus_handler* handler);

/*=============================================================================
---函数：sensor_sampling_all(sensor_data* data)
---功能：同时采样温度以及压力
---输入：data		- 采样数据样本
---输出：
-----------------------------------------------------------------------------*/
void sensor_sampling_all(sensor_data* data);
/*=============================================================================
---函数：sensor_sampling_press(sensor_data* data)
---功能：仅采样压力
---输入：data		- 采样数据样本
---输出：
---bug:spi方式下不建议使用，不稳定
-----------------------------------------------------------------------------*/
//void sensor_sampling_press(sensor_data* data);

#endif //__WFSENSOR_H__
